var ctx;
var name = "";
var type = 0;
var firstLevel;
var secondLevel;
var selectFirstLevel;
var selectSecondLevel;
var min;
var max;

function setCtx(ctx) {
    this.ctx = ctx;
}
function gotoPage(page) {
    name = ifNull($("#name").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }
    loadContent("/goods/goodsInfoList?pNo=" + page + "&name=" + name);
}

function ifNull(mess) {
    if (mess == null || mess == undefined) {
        mess = "";
    }
    return mess;
}

// 条件搜索用户列表
function getGoodsInfoList() {
    var goodsType = $("#goodsType").val();
    var state = -1;
    firstLevel = $("#firstLevel").val();
    secondLevel = ifNull($("#secondLevel").val());
    name = ifNull($("#name").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }
    if (minP == $("#minFilter").attr("placeholder")) {
        minP = "";
    }
    if (maxP == $("#maxFilter").attr("placeholder")) {
        maxP = "";
    }
    var minP = $("#minFilter").val();
    var maxP = $("#maxFilter").val();

    min = minP;
    max = maxP;

    if (minP > maxP) {
        layer.tips('最高价不得低于最低价!', '#maxFilter', {
            tips : 3
        });
        return false;
    } else {
        loadContent("/goods/goodsInfoList?pNo=1&name=" + name + "&state="
                + state + "&goodsType=" + goodsType + "&firstLevel="
                + firstLevel + "&secondLevel=" + secondLevel + "&min=" + min
                + "&max=" + max);

    }

}

function gotoGoodsInfoPage(page) {
    if (page == null) {
        page = $(".inputclass").val();
    }
    loadContent("/goods/goodsInfoList?pNo=" + page + "&name=" + cellName);
}

// 新增或编辑用户(1:新增 2:编辑)
function saveOrUpdate(id) {
    if (id != null) {

        loadContent("/goods/editGoodsInfo?id=" + id);

    } else {
        loadContent("/goods/editGoodsInfo?id=0");
    }

}

var id;
function changeCategory(obj) {

    id = $("#firstLevel").val();

    if (id != null || id != '') {
        $("#secondId").show();
        // $("#searchBox").css("width","275px");
    }
    if (id == null || id == '' || id == undefined) {
        $("#secondId")[0].style.display = "none";
        // $("#searchBox").css("width","419px");
    }
    $("#selectFirstLevel").val($("#firstLevel").val());
    if (id != '') {
        $.get(ctx + "/goods/changeCategory", {
            id : id
        }, function(data) {
            data = JSON.parse(data);
            if (data.success) {

                var result = "<option value='"
                        + "'>&nbsp;&nbsp;&nbsp;分类</option>";
                $.each(data.obj, function(index, item) {
                    if (item.id == selectSecondLevel) {
                        result += "<option id='" + item.name + "'" + " value='"
                                + item.id
                                + "'selected='selected'>&nbsp;&nbsp;&nbsp;"
                                + item.name + "</option>";
                    } else {
                        result += "<option id='" + item.name + "'" + " value='"
                                + item.id + "'>&nbsp;&nbsp;&nbsp;" + item.name
                                + "</option>";
                    }
                });
                $("#secondLevel").html('');
                $("#secondLevel").append(result);
                $("#secondLevel").change();

            }
        });
    }
}

$(function() {
    selectFirstLevel = $("#selectFirstLevel").val();

    if (selectFirstLevel != null || selectFirstLevel != '') {
        $("#secondId").show();
    }
    if (selectFirstLevel == null || selectFirstLevel == ''
            || selectFirstLevel == undefined) {
        $("#secondId")[0].style.display = "none";
        // $("#searchBox").css("width","419px");
    }
    selectSecondLevel = $("#selectSecondLevel").val();
    if (selectFirstLevel != null) {
        $.ajax({
            type : "GET",
            url : ctx + "/goods/changeCategory",
            cache : false,
            dataType : 'json',
            data : {
                id : selectFirstLevel
            },
            success : function(data) {
                if (data.success) {
                    var result = "<option value='" + "'>分类</option>";
                    $.each(data.obj, function(index, item) {
                        if (item.id == selectSecondLevel) {
                            result += "<option id='" + item.name + "'"
                                    + " value='" + item.id
                                    + "'selected='selected'>" + item.name
                                    + "</option>";
                        } else {
                            result += "<option id='" + item.name + "'"
                                    + " value='" + item.id + "'>" + item.name
                                    + "</option>";
                        }
                    });
                    $("#secondLevel").html('');
                    $("#secondLevel").append(result);
                    $("#secondLevel").change();
                }
            }
        });
    }
})
// 全选和反选
function selectAll(target) {
    if (target.checked) {
        $("#tableTbody :checkbox").attr("checked", true);
        $(target).parents("thead").siblings("tbody").children().children()
                .each(function() {
                    $(this).css("background", "#D3F0F1");
                });
    } else {
        $("#tableTbody :checkbox").attr("checked", false);
        $(target).parents("thead").siblings("tbody").children().children()
                .each(function() {
                    $(this).css("background", "");
                });
    }
}

// 鼠标滑入一行改变背景颜色
function mouseoverOne(target) {
    $(target).children().each(function() {
        $(this).css("background", "#D3F0F1");
    });
}

// 鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target) {
    $(target).children().each(function() {
        $(this).css("background", "");
    });
}

// 选择或取消选中当前记录
function selectOne(id, target) {
    if (target.checked) {
        $("#goods" + id).attr("checked", true);
        $(target).parents("tr").children().each(function() {
            $(this).css("background", "#D3F0F1");
        });
    } else {
        $("#goods" + id).attr("checked", false);
        $(target).parents("tr").children().each(function() {
            $(this).css("background", "");
        });
    }
}

function updateState(id, state) {
    var mess = "";
    if (state == 1) {
        mess = "请确认上架该商品!";
    } else {
        mess = "请确认下架该商品";
    }
    layer.confirm(mess, {
        icon : 3,
        title : '提示'
    }, function(index) {
        // do something
        $.get(ctx + "/goods/updateState", {
            id : id,
            state : state
        }, function(data) {
            data = JSON.parse(data);
            if (data.success) {
                layer.msg("操作成功", {
                    icon : 1,
                    time : 2000
                // 2秒关闭（如果不配置，默认是3秒）
                }, function() {
                    loadContent("/goods/goodsInfoList?goodsType="
                            + data.obj.type);
                });
            } else {
                layer.msg(data.msg, {
                    icon : 2,
                    time : 2000
                // 2秒关闭（如果不配置，默认是3秒）
                });
            }
        });
        layer.close(index);
    });
}

function deleteByIds() {
    var goodsType = $("#goodsType").val();
    if ($("img[name='goodsChecked'][src*=honggou]").length == 0) {
        layer.msg("请选择要删除的记录!", {
            icon : 3,
            time : 2000
        // 2秒关闭（如果不配置，默认是3秒）
        });
    } else {
        var delIds = [];
        $("#tableTbody img[name='goodsChecked'][src*=honggou]").each(
                function() {
                    delIds.push($(this).attr("class"));
                });
        var vals = delIds.join(',');//转换为逗号隔开的字符串 
        layer.confirm('确认删除?', {
            icon : 3,
            title : '提示'
        }, function(index) {
            //do something
            $.post(ctx + "/goodsInfo/deleteGoodsInfo", {
                goodsIDs : vals
            }, function(data) {
                data = JSON.parse(data);
                if (data.success) {
                    layer.msg("删除成功", {
                        icon : 1,
                        time : 2000
                    //2秒关闭（如果不配置，默认是3秒）
                    }, function() {
                        loadContent("/goodsInfo/goodsInfoList?goodsType="
                                + goodsType);
                    });
                } else {
                    layer.msg(data.msg, {
                        icon : 2,
                        time : 2000
                    //2秒关闭（如果不配置，默认是3秒）
                    });
                }
            });
            layer.close(index);
        });

    }
}

function updateRecommond(goodsId) {
    if (goodsId != null) {
        $.get(ctx + "/goods/updateSort", {
            id : goodsId
        }, function(data) {
            data = JSON.parse(data);
            if (data.success) {
                layer.msg("操作成功", {
                    icon : 1,
                    time : 2000
                //2秒关闭（如果不配置，默认是3秒）
                }, function() {
                    loadContent("/goods/goodsInfoList");
                });
            } else {
                layer.msg(data.msg, {
                    icon : 2,
                    time : 2000
                //2秒关闭（如果不配置，默认是3秒）
                });
            }
        });
    }
}

function showDetail(id) {
    loadContent("/goods/showDetail?id=" + id);
}

//实现字段较长时的隐藏于显示
var index;
function mouseOverAddr(item , addr){
    index = layer.tips(addr, '#addr' + item, {
          tips: [3, '#37CED4']
        });
}

function mouseOutAddr(item , addr){
    layer.close(index);
}